/**
 * 
 */
package com.gr.staffpm.pages.dashboard.gadgets;

import java.util.ArrayList;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Instant;
import org.joda.time.Period;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.model.StringModel;
import com.gr.staffpm.tasks.model.TaskPanel;


/**
 * @author Graham Rhodes 5 Apr 2011 15:58:23
 */
public class TasksDuePanel extends Panel {

    private static final long serialVersionUID = 1L;

    public TasksDuePanel(String id, DateTime tasksDate, ArrayList<Task> tasksDue) {
        super(id);

        Label tasksTitle = new Label("tasksTitle", new StringModel(""));
        Period diff = new Period(new Instant(), tasksDate);
        DateTimeFormatter fmt;

        if (diff.toStandardHours().isLessThan(Hours.hours(24))) {
            tasksTitle.setDefaultModel(new StringModel("Today"));
        } else if (diff.toStandardDays().isLessThan(Days.SEVEN)) {
            fmt = DateTimeFormat.forPattern("EEEEE");
            tasksTitle.setDefaultModel(new StringModel(fmt.print(tasksDate)));
        } else {
            fmt = DateTimeFormat.forPattern("dd MMMM yyyy");
            tasksTitle.setDefaultModel(new StringModel(fmt.print(tasksDate)));
        }
        add(tasksTitle);

        RepeatingView tasks = new RepeatingView("tasks");
        for (Task task : tasksDue) {
            tasks.add(new TaskPanel(String.valueOf(task.getTaskId()), task));
        }
        add(tasks);

    }

}
